package com.ihsan.kocak;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SendMultiSMSActivity extends Activity {
	TextView tvOut;
	Button btnOk;

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it
	 *            is null.</b>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// find View-elements
		tvOut = (TextView) findViewById(R.id.tvOut);
		tvOut.setMovementMethod(new ScrollingMovementMethod());
		btnOk = (Button) findViewById(R.id.btnOk);

		OnClickListener oclBtnOk = new OnClickListener() {

			public void onClick(View v) {
				

				List<Contact> contacts = getContacts();
//				tvOut.setText("");
//				for (Contact c : contacts) {
//					tvOut.setText(tvOut.getText()  + c.getAsText()+"\n");
//				}
				sendMultiMessage(contacts, tvOut.getText().toString());
				
				
				 

			
			}

		};

		// assign click listener to the OK button (btnOK)
		btnOk.setOnClickListener(oclBtnOk);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(com.ihsan.kocak.R.menu.main, menu);
		return true;
	}

	public List<Contact> getContacts() {
		Cursor phones1 = getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
				null, null);
		List<Contact> contactsHandSet = new ArrayList<Contact>();
		while (phones1.moveToNext()) {
			String name = phones1
					.getString(phones1
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			String phoneNumber = phones1
					.getString(phones1
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			contactsHandSet.add(new Contact(name, phoneNumber));
		}
		phones1.close();
		return contactsHandSet;

	}
	public void sendMultiMessage(List <Contact> contacts,String message){
		tvOut.setText("");
		String salute="";
		if(Locale.getDefault().getDisplayLanguage().equals("Turkish")){
			salute="Sayın";
		}
		else{
			salute="Dear";
		}
		for(Contact c:contacts){
			sendSms(c.getPhoneNumber(), salute+ " "+c.getDisplayName()+" "+message);
			
		}
	}
	public void sendSms(String number,String message){
		 SmsManager sms = SmsManager.getDefault();
       sms.sendTextMessage(number, null, message, null, null);
		
	}

}
